package com.imooc.course.server.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imooc.course.server.dao.ChapterDao;
import com.imooc.course.server.dto.ChapterDto;
import com.imooc.course.server.dto.ChapterPageDto;
import com.imooc.course.server.entity.Chapter;
import com.imooc.course.server.entity.ChapterExample;
import com.imooc.course.server.service.ChapterService;
import com.imooc.course.server.utils.CopyUtil;
import com.imooc.course.server.utils.UuidUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service("chapterService")
public class ChapterServiceImpl implements ChapterService {

    private static final Logger log = LoggerFactory.getLogger(ChapterServiceImpl.class);

    @Resource(name = "chapterDao")
    private ChapterDao chapterDao;

    /**
     * 列表查询
     * @param chapterPageDto
     */
    @Override
    public void getList(ChapterPageDto chapterPageDto) {
        PageHelper.startPage(chapterPageDto.getPage(),chapterPageDto.getSize());

        ChapterExample chapterExample = new ChapterExample();
        //chapterExample.createCriteria()方法只能使用一次，获得一个Criteria对象。
        // 一个Criteria对象实例能多次调用and系列方法增加多个不同的查询条件。
        //切忌：
        /*直接多次使用chapterExample.createCriteria()方法去
          调用and系列方法增加多个不同的查询条件是无效的，
          最后一次and方法会覆盖前面所有的and方法的查询条件。*/
        ChapterExample.Criteria criteria = chapterExample.createCriteria();
        if (!StringUtils.isEmpty(chapterPageDto.getCourseId())) {
            criteria.andCourseIdEqualTo(chapterPageDto.getCourseId());
        }
        List<Chapter> chapterList = chapterDao.selectByExample(chapterExample);

        PageInfo<Chapter> chapterPageInfo = new PageInfo<>(chapterList);
        chapterPageDto.setTotal(chapterPageInfo.getTotal());

        List<ChapterDto> chapterDtoList = CopyUtil.copyList(chapterList, ChapterDto.class);

        chapterPageDto.setList(chapterDtoList);
    }

    /**
     * 保存，id有值为更新，id无值为新增
     * @param chapterDto
     */
    @Override
    public void save(ChapterDto chapterDto) {
        Chapter copy = CopyUtil.copy(chapterDto, Chapter.class);
        String id = chapterDto.getId() != null ? chapterDto.getId().trim() : chapterDto.getId();
        if (StringUtils.isEmpty(id)) {
            this.addChapter(copy);
        }else{
            this.updateChapter(copy);
        }
    }

    private void updateChapter(Chapter copy) {
        chapterDao.updateByPrimaryKey(copy);
    }

    private void addChapter(Chapter copy) {
        copy.setId(UuidUtil.getShortUuid());
        chapterDao.insert(copy);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void deleteChapter(String id) {
        chapterDao.deleteByPrimaryKey(id);
    }

    @Override
    public List<ChapterDto> getListByCourseId(String courseId) {
        ChapterExample example = new ChapterExample();
        example.createCriteria().andCourseIdEqualTo(courseId);
        List<Chapter> chapterList = chapterDao.selectByExample(example);
        return CopyUtil.copyList(chapterList,ChapterDto.class);
    }
}
